var ws = m_id = member_id = rec_id = positions = spage = store_id = '';
var g_jingle = ws = mobile_body = images = cate_name = cate_id = g_price = image_path = g_name = '';
document.addEventListener('plusready',function(){
	member_id = plus.storage.getItem('id');
	store_id = plus.storage.getItem('store_id');
	ws = plus.webview.currentWebview();
	cart_id = ws.cart_id;
	cart_name = ws.cart_name;
	spage = ws.spage;
	zt = ws.zt;
	console.log(cart_id+'--'+cart_name);
	//处理
	$('.goods_body').on('tap',function(){
		positions = $(this).attr('data-type');
		plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]},function(e){
			if(e.index == 1){
				var car =plus.camera.getCamera();
				car.captureImage(function(path){
					//展示图片
//					alert(plus.io.convertLocalFileSystemURL(path));
//					$('.shangchuan').attr('src','file://' + plus.io.convertLocalFileSystemURL(path));
					appendpic(path);
					
				},function(err){});
				
			}else if(e.index == 2){
				plus.gallery.pick(function(path){
//					$('.shangchuan').attr('src',path);
					appendpic(path);
				});
			}
		});
	})
				
//				
	$('.release').on('tap',function(){
		g_jingle = $('.g_jingle').val();
		g_price = $('.g_price').val();
		g_name = $('.g_name').val();
		$('.mobile_body').each(function(){
			$(this).find('.evapic').each(function(){
				mobile_body += $(this).attr('src') + ',';
			});
		});
		$('.images').each(function(){
			var _this = $(this);
			$(this).find('.evapic').each(function(){
				var str = $(this).attr('src');
				var index = str.lastIndexOf("\/");  
				images += str.substring(index + 1, str.length) + ',';
				image_path = str.substring(index + 1, str.length);
			})
		}); 
		if(!g_price.match(numb)){ 
			toast('物品价格请输入纯数字');return; 
		}
//		console.log(g_jingle+'--'+member_id+'--'+g_price+'--'+cart_id+'--'+g_name+'--'+cart_name+'--'+mobile_body+'--'+store_id+'--'+images+'--'+image_path);
		$.ajax({
			type:"post",
			url:apiRoot + "?act=goods&op=savegoods",
			data:{
				m_id:member_id,
				g_jingle:g_jingle,
				g_price:g_price,
				g_name:g_name,
				mobile_body:mobile_body,
				images:images,
				cate_id:cart_id,
				cate_name:cart_name,
				store_id:store_id,
				image_path:image_path
			},
			dataType:'json',
			success:function(data){
				console.log(JSON.stringify(data));
				if(data.code == 200){
					toast('发布成功！');
						plus.webview.getWebviewById(spage).reload();
						setTimeout(function(){
							ws.close();
						},30)
				}else{
					toast(data.datas.error);
				}
			}, 
			error:function(e){
				console.log(JSON.stringify(e));
				error();
			}
		});
	})
	
})
//添加图片
var index = 1;
var files = [];
function appendpic(p){
	plus.nativeUI.showWaiting('图片处理中...');
//		console.log(p); 
	//创建新的路径
//				var newpath = p.replace(/\./g , new Date().gettime() + '.');
	var newpath = p.replace(/\./g , new Date().getTime()+'.');
	
	plus.zip.compressImage({
			src : p,
			dst : newpath,
			quality : 20
		},
		function(){//毁掉成功
			files.push({name : "uploadkey" + index,path:p});
			index++;
			upload();
		},
		function(error){
		alert('压缩图片失败');
		plus.nativeUI.closeWaiting();
	})
}

function upload(){
		if(files.length <= 0){ 
			toast("请上传头像！");
		}
//				console.log(JSON.stringify(files));return;
		var server = apiRoot + "?act=sns_album&op=file_uploadone&member_id="+member_id+"&store_id="+store_id;  
		var task = plus.uploader.createUpload(server,
		    {method:"post"},   
			function(t,status){ //上传完成 
				if(status == 200){ 
					plus.nativeUI.closeWaiting();
					var infopics = $.parseJSON(t.responseText);
					console.log(JSON.stringify(infopics));
					if(infopics.code == 200){
						$('.'+positions).prepend('<img class="evapic" src="'+ pics + store_id + '/' + infopics.datas.file_name +'" data-preview-src="" data-preview-group="1"/>');
					}else{
						toast(infopics.datas.error);
					} 
	//					plus.nativeUI.toast('处理完成' + t.responseText);
				}else{      
					toast('上传失败：' + status);
					plus.nativeUI.closeWaiting();
				} 
				   
			},function (e) {
				console.log(JSON.stringify(e));
				plus.nativeUI.closeWaiting();
			}
			
		); 
		for(var i = 0;i<files.length ; i++){
			var f =files[i];
			task.addFile(f.path,{key:'file'});
	//					    console.log(f.name)
		}
		task.start();
	 
}		
